<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
use app\common\model\AdminRule;
use app\common\model\AdminUserGroup;
use app\common\model\AdminUsers;
use app\common\model\AdminNav;

use think\Loader;


	/**
	 * 后台管理首页
	 * @author Yel 
	 * @copyright  yel
	 */
class Index extends AdminBase
{	


	/**
	 * 欢迎页面
	 * @author Yel  2019/6/28
	 * @copyright  yel
	 */
	public function welcome(){
		return view();
	}

	/**
	 * 主要页面 
	 * @author Yel  2019/6/28
	 * @copyright  yel
	 */
	public function index(){
		//获取菜单列表
		$Nav  = $this->getNavList();
		
		//获取权限列表
		$Rule  = $this->getUserRuleId();
		
		//开辟空间
		$NavList = []; //最后输出的菜单列表
		foreach ($Nav as $key => $value) {
			foreach ($Rule as $k => $v) {
				if($v['mac'] == $value['mac']){
					$NavList[] = $value;
					break;
				}
			}
		}
		
		$this->assign('data',$NavList);
		return view();
	}


	/**
	 * 菜单列表
	 * @author Yel  2019/6/27
	 * @copyright  yel
	 */
	private function getNavList(){
		//实例化
		$nav = new AdminNav();

		//查询所有数据
		$data = $nav->selectData([],'id,name,ico,mac,pid','sort desc,id asc');

		//引入拓展类
		Loader::import('Org.Arraysets',EXTEND_PATH);

		//实例化
		$Arraysets = new \Arraysets;

		//按照树的形式输出
		$data = $Arraysets->getTree($data);

		//判断是否有子级
		foreach ($data as $key => $value) {
			$data[$key]['child'] = $Arraysets->haveChild($value['id'],$data,'pid');
		}
		return $data;
	}



	/**
	 * 获取用户的权限
	 * @author Yel  2019/7/5
	 * @copyright  yel
	 * @return array  返回自己的权限列表
	 */
	private function getUserRuleId(){
		//获取用户数据
		$user = session('user');
		//获取用户角色
		$User = new AdminUsers();
		$userInfo = $User->findData($user['id'],'admin_user_group_id');
		if(empty($userInfo)){
			$this->redirect('admin/login/index');
		}
		$group_id = $userInfo['admin_user_group_id'];

		//获取自己角色的权限
		$Group = new AdminUserGroup();
		$groupInfo = $Group->findData($group_id,'rule,status');
		if(empty($groupInfo)){
			$this->redirect('admin/login/index',['login_error'=>true]);
		}
		if($groupInfo['status'] == 0){
			$this->redirect('admin/login/index',['login_error'=>true]);
		}
		//根据自己角色权限ID 获取权限值
		$Rule = new AdminRule();
		$ruleInfo = $Rule->selectData(['id'=>['in',$groupInfo['rule']]]);
		return $ruleInfo;
	}

}